package com.jack.helloworld.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.jack.helloworld.R;

public class CalculaterActivity extends Activity implements View.OnClickListener {

    private EditText etInput;

    private Button btn_0;
    private Button btn_1;
    private Button btn_2;
    private Button btn_3;
    private Button btn_4;
    private Button btn_5;
    private Button btn_6;
    private Button btn_7;
    private Button btn_8;
    private Button btn_9;
    private Button btn_clear;
    private Button btn_divide;
    private Button btn_multiple;
    private Button btn_delete;
    private Button btn_plus;
    private Button btn_minus;
    private Button btn_equal;
    private Button btn_point;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculater);

        initView();
        initData();

    }

    private void initView() {

        etInput = (EditText) findViewById(R.id.et_input);
        btn_0 = (Button) findViewById(R.id.btn_0);
        btn_1 = (Button) findViewById(R.id.btn_1);
        btn_2 = (Button) findViewById(R.id.btn_2);
        btn_3 = (Button) findViewById(R.id.btn_3);
        btn_4 = (Button) findViewById(R.id.btn_4);
        btn_5 = (Button) findViewById(R.id.btn_5);
        btn_6 = (Button) findViewById(R.id.btn_6);
        btn_7 = (Button) findViewById(R.id.btn_7);
        btn_8 = (Button) findViewById(R.id.btn_8);
        btn_9 = (Button) findViewById(R.id.btn_9);
        btn_clear = (Button) findViewById(R.id.btn_clear);
        btn_divide = (Button) findViewById(R.id.btn_divide);
        btn_multiple = (Button) findViewById(R.id.btn_multiple);
        btn_delete = (Button) findViewById(R.id.btn_delete);
        btn_plus = (Button) findViewById(R.id.btn_plus);
        btn_minus = (Button) findViewById(R.id.btn_minus);
        btn_equal = (Button) findViewById(R.id.btn_equal);
        btn_point = (Button) findViewById(R.id.btn_point);

    }

    private void initData() {
        btn_0.setOnClickListener(this);
        btn_1.setOnClickListener(this);
        btn_2.setOnClickListener(this);
        btn_3.setOnClickListener(this);
        btn_4.setOnClickListener(this);
        btn_5.setOnClickListener(this);
        btn_6.setOnClickListener(this);
        btn_7.setOnClickListener(this);
        btn_8.setOnClickListener(this);
        btn_9.setOnClickListener(this);
        btn_clear.setOnClickListener(this);
        btn_divide.setOnClickListener(this);
        btn_multiple.setOnClickListener(this);
        btn_delete.setOnClickListener(this);
        btn_plus.setOnClickListener(this);
        btn_minus.setOnClickListener(this);
        btn_equal.setOnClickListener(this);
        btn_point.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        String etString = etInput.getText().toString();
        switch (view.getId()) {
            case R.id.btn_0:
            case R.id.btn_1:
            case R.id.btn_2:
            case R.id.btn_3:
            case R.id.btn_4:
            case R.id.btn_5:
            case R.id.btn_6:
            case R.id.btn_7:
            case R.id.btn_8:
            case R.id.btn_9:
            case R.id.btn_point:
                etInput.setText(etString + ((Button)view).getText().toString());
                break;
            case R.id.btn_plus:
            case R.id.btn_minus:
            case R.id.btn_multiple:
            case R.id.btn_divide:
                etInput.setText(etString + " " + ((Button)view).getText().toString() + " ");
                break;
            case R.id.btn_equal:
                getResult();
                break;
            case R.id.btn_delete:
                if (etString == null || etString.equals("")) {
                    return;
                }
                etInput.setText(etString.substring(0, etString.length() - 1));
                break;
            case R.id.btn_clear:
                etInput.setText("");
                break;


        }

    }

    private void getResult() {

        String etString = etInput.getText().toString();
        if (etString == null || etString.equals("")) {
            return;
        }
        if (!etString.contains(" ")) {
            return;
        }

        double result = 0;
        String str1 = etString.substring(0, etString.indexOf(" "));
        String op = etString.substring(etString.indexOf(" ") + 1, etString.indexOf(" ") + 2);
        String str2 = etString.substring(etString.indexOf(" ") + 3);

        if (!str1.equals("") && !str2.equals("")) {
            double d1 = Double.parseDouble(str1);
            double d2 = Double.parseDouble(str2);

            if (op.equals("+")) {
                result = d1 + d2;
            } else if (op.equals("-")) {
                result = d1 - d2;
            } else if (op.equals("×")) {
                result = d1 * d2;
            } else if (op.equals("÷")) {
                if (d2 == 0) {
                    result = 0;
                } else {
                    result = d1 / d2;
                }
            }
            if (!str1.contains(".") && !str2.contains(".")) {
                int r = (int) result;
                etInput.setText(r + "");
            } else {
                etInput.setText(result + "");
            }

        } else if (!str1.equals("") && str2.equals("")) {
            return;
        } else if (str1.equals("") && !str2.equals("")) {
            double d2 = Double.parseDouble(str2);

            if (op.equals("+")) {
                result = d2;
            } else if (op.equals("-")) {
                result =  0 - d2;
            } else if (op.equals("×")) {
                result = 0;
            } else if (op.equals("÷")) {
                result = 0;
            }
            etInput.setText(result + "");
        } else {
            etInput.setText("");
        }


    }
}
